package com.wjk.easy.ui.login;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import android.util.Patterns;

import com.wjk.easy.data.LoginDataSource;
import com.wjk.easy.data.Result;
import com.wjk.easy.data.model.LoggedInUser;
import com.wjk.easy.R;
import com.wjk.easy.db.LastUserBean;

public class LoginViewModel extends ViewModel {

    private MutableLiveData<LoginFormState> loginFormState = new MutableLiveData<>();
    private MutableLiveData<LoginResult> loginResult = new MutableLiveData<>();
    private LoginDataSource dataSource;

    LoginViewModel() {
        dataSource = new LoginDataSource();
    }

    LiveData<LoginFormState> getLoginFormState() {
        return loginFormState;
    }

    LiveData<LoginResult> getLoginResult() {
        return loginResult;
    }

    public void login(String username, String password) {
        // can be launched in a separate asynchronous job
        // handle login
        Result<LastUserBean> result = dataSource.login(username, password);

        if (result instanceof Result.Success) {
            LastUserBean data = ((Result.Success<LastUserBean>) result).getData();
            loginResult.setValue(new LoginResult(data));
        } else {
            String errMsg = ((Result.Error)result).getError().getMessage();
            loginResult.setValue(new LoginResult(errMsg));
        }
    }

    public void regist(String username, String password) {
        // can be launched in a separate asynchronous job
        // handle login
        Result<LastUserBean> result = dataSource.regist(username, password);

        if (result instanceof Result.Success) {
            LastUserBean data = ((Result.Success<LastUserBean>) result).getData();
            loginResult.setValue(new LoginResult(data));
        } else {
            String errMsg = ((Result.Error)result).getError().getMessage();
            loginResult.setValue(new LoginResult(errMsg));
        }
    }

    public void resetPwd(String username, String password) {
        // can be launched in a separate asynchronous job
        // handle login
        Result<LastUserBean> result = dataSource.resetPwd(username, password);

        if (result instanceof Result.Success) {
            LastUserBean data = ((Result.Success<LastUserBean>) result).getData();
            loginResult.setValue(new LoginResult(data));
        } else {
            String errMsg = ((Result.Error)result).getError().getMessage();
            loginResult.setValue(new LoginResult(errMsg));
        }
    }

    void loginDataChanged(String username, String password) {
        if (!isUserNameValid(username)) {
            loginFormState.setValue(new LoginFormState(R.string.invalid_username, null));
        } else if (!isPasswordValid(password)) {
            loginFormState.setValue(new LoginFormState(null, R.string.invalid_password));
        } else {
            loginFormState.setValue(new LoginFormState(true));
        }
    }

    // A placeholder username validation check
    private boolean isUserNameValid(String username) {
        if (username == null) {
            return false;
        }
        if (username.contains("@")) {
            return Patterns.EMAIL_ADDRESS.matcher(username).matches();
        } else {
            return !username.trim().isEmpty();
        }
    }

    // A placeholder password validation check
    private boolean isPasswordValid(String password) {
        return password != null && password.trim().length() >= 5;
    }
}
